package cn.dzhang.rcp.hyperbola;

import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import cn.dzhang.rcp.hyperbola.common.UIConstants;
import cn.dzhang.rcp.hyperbola.common.UIUtils;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

	private TrayItem trayItem;
	private ApplicationActionBarAdvisor actionBarAdvisor;

	public ApplicationWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	@Override
	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		actionBarAdvisor = new ApplicationActionBarAdvisor(configurer);
		return actionBarAdvisor;
	}

	@Override
	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		// configurer.setTitle("Hyperbola");
		configurer.setInitialSize(new Point(800, 600));
		configurer.setShowMenuBar(true);
		configurer.setShowCoolBar(true);
		configurer.setShowStatusLine(true);
		// configurer.setShowProgressIndicator(true);
		configurer.setShowPerspectiveBar(true);
		// configurer.setShowFastViewBars(true);
	}

	@Override
	public void postWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		Shell shell = configurer.getWindow().getShell();
		UIUtils.setDefaultBackgroundColor(shell);

		initStatusline();
		initSystemTray();
	}

	private void initSystemTray() {
		final IWorkbenchWindow window = getWindowConfigurer().getWindow();
		trayItem = initTrayItem(window);

		if (null != trayItem) {
			hookPopupMenu(window);
			hookMinimize(window);
		}

	}

	@Override
	public boolean preWindowShellClose() {
		// Shell shell = getWindowConfigurer().getWindow().getShell();
		// MessageDialog.openConfirm(shell, "退出提示", "提出还是最小化");

		// shell.notifyListeners(SWT.Iconify, new Event());

		return true;
	}

	private void hookMinimize(final IWorkbenchWindow window) {
		final Shell shell = window.getShell();
		shell.addShellListener(new ShellAdapter() {
			@Override
			public void shellIconified(ShellEvent e) {
				shell.setVisible(false);
			}
		});

		trayItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				if (!shell.isVisible()) {
					shell.setVisible(true);
				}
				shell.setMinimized(false);
				shell.setActive();
			}
		});

	}

	private void hookPopupMenu(final IWorkbenchWindow window) {
		trayItem.addMenuDetectListener(new MenuDetectListener() {

			@Override
			public void menuDetected(MenuDetectEvent event) {
				MenuManager trayMenu = new MenuManager();
				Menu menu = trayMenu.createContextMenu(window.getShell());
				actionBarAdvisor.fillTrayItem(trayMenu);
				menu.setVisible(true);
			}
		});
	}

	private TrayItem initTrayItem(IWorkbenchWindow window) {
		Tray tray = window.getShell().getDisplay().getSystemTray();
		if (null == tray) {
			return null;
		}

		TrayItem trayItem = new TrayItem(tray, SWT.NONE);
		trayItem.setImage(UIConstants.Images.USER_32);
		trayItem.setToolTipText("Hyperbola");

		return trayItem;
	}

	private void initStatusline() {
		Image statusImage = UIConstants.Images.ONLINE;
		IStatusLineManager statusline = getWindowConfigurer()
				.getActionBarConfigurer().getStatusLineManager();
		statusline.setMessage(statusImage, "Online");
	}

}
